/*
 * AS3 MultiTouch Client
 *
 * DrawOutlineBehaviour - Draws the touchable object's outline when touched or dragged.  
 *
 * Copyright 2008 Artifacts - Fine Software Development
 * http://www.artifacts.de
 * Author: Michael Markowski (m.markowski@artifacts.de)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

/**
 * DrawOutlineBehaviour - Draws the touchable object's outline when touched or dragged.  
 */
package de.artifacts.multitouch.behaviours
{
	import de.artifacts.multitouch.Cursor;
	import de.artifacts.multitouch.TouchEvent;
	import de.artifacts.multitouch.TouchManager;
	
	import flash.display.Sprite;
	
	public class DrawOutlineBehaviour extends TouchBehaviour
	{
		private var outlineSprite : Sprite;
		
		public function DrawOutlineBehaviour()
		{
			super();
			this.outlineSprite = new Sprite();
		}

		override public function touchedHandler(event:TouchEvent):void
		{
			var cursor : Cursor = touchManager.firstHittingCursor(touchable);
			if (cursor != null)
			{
				var x : Number = touchable.wrappee.getBounds(touchManager.touchArea).x;
				var y : Number = touchable.wrappee.getBounds(touchManager.touchArea).y;
				var w : Number = touchable.wrappee.getBounds(touchManager.touchArea).width;
				var h : Number = touchable.wrappee.getBounds(touchManager.touchArea).height;
				
				outlineSprite.graphics.clear();
				outlineSprite.graphics.lineStyle(1, 0x0000ff);			
				outlineSprite.graphics.drawRect(x,y,w,h);
				
				touchManager.touchArea.addChild(outlineSprite);				
			}
		}
		
		override public function releasedHandler(event : TouchEvent) : void
		{
			super.releasedHandler(event);
			try {
				touchManager.touchArea.removeChild(outlineSprite);
			} catch (e : Error) {}
		}
		
	}
}